package com.allen.study.infrastructure.persistence;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.allen.study.application.api.request.LeaveApplyQueryRequest;
import com.allen.study.application.api.response.LeaveApplyQueryResponse;
import com.allen.study.application.repository.ILeaveApplyReadModelRepo;
import com.allen.study.domain.entity.LeaveApply;
import com.allen.study.domain.repository.ILeaveApplyRepo;
import com.allen.study.common.base.Pagination;
import com.allen.study.infrastructure.persistence.assembler.LeaveApplyPOAssembler;
import com.allen.study.infrastructure.persistence.dao.ILeaveApplyMapper;
import com.allen.study.infrastructure.persistence.po.LeaveApplyPO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 请假申请表
 *
 * @author AllenSun
 * @since 2025-04-23 20:46
 */
@Service
@AllArgsConstructor
public class LeaveApplyRepoImpl extends ServiceImpl<ILeaveApplyMapper, LeaveApplyPO>
        implements ILeaveApplyRepo, ILeaveApplyReadModelRepo {

    /**
     * 请假申请表持久化对象结构映射
     */
    private final LeaveApplyPOAssembler leaveApplyPOAssembler;

    @Override
    public List<LeaveApplyQueryResponse> query(Pagination pagination, LeaveApplyQueryRequest queryRequest) {
        // 构造查询条件
        LambdaQueryWrapper<LeaveApplyPO> lambdaQueryWrapper = Wrappers.lambdaQuery();


        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getEmployeeId())) {
            lambdaQueryWrapper.eq(LeaveApplyPO::getEmployeeId, queryRequest.getEmployeeId());
        }
        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getStartDate())) {
            lambdaQueryWrapper.eq(LeaveApplyPO::getStartDate, queryRequest.getStartDate());
        }
        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getEndDate())) {
            lambdaQueryWrapper.eq(LeaveApplyPO::getEndDate, queryRequest.getEndDate());
        }
        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getDays())) {
            lambdaQueryWrapper.eq(LeaveApplyPO::getDays, queryRequest.getDays());
        }
        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getReason())) {
            lambdaQueryWrapper.eq(LeaveApplyPO::getReason, queryRequest.getReason());
        }
        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getHandoverPerson())) {
            lambdaQueryWrapper.eq(LeaveApplyPO::getHandoverPerson, queryRequest.getHandoverPerson());
        }
        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getLeaderId())) {
            lambdaQueryWrapper.eq(LeaveApplyPO::getLeaderId, queryRequest.getLeaderId());
        }
        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getPmId())) {
            lambdaQueryWrapper.eq(LeaveApplyPO::getPmId, queryRequest.getPmId());
        }
        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getSupervisorId())) {
            lambdaQueryWrapper.eq(LeaveApplyPO::getSupervisorId, queryRequest.getSupervisorId());
        }
        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getProcessInstanceId())) {
            lambdaQueryWrapper.eq(LeaveApplyPO::getProcessInstanceId, queryRequest.getProcessInstanceId());
        }
        if (ObjectUtils.isNotEmpty(queryRequest.getProcessInstanceIds())) {
            lambdaQueryWrapper.in(LeaveApplyPO::getProcessInstanceId, queryRequest.getProcessInstanceIds());
        }
        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getStatus())) {
            lambdaQueryWrapper.eq(LeaveApplyPO::getStatus, queryRequest.getStatus());
        }

        // 默认按照主键倒序排序
        lambdaQueryWrapper.orderByDesc(LeaveApplyPO::getId);

        // 分页查询
        Page<LeaveApplyPO> page = super.page(Page.of(pagination.getPageNumber(), pagination.getPageSize()), lambdaQueryWrapper);
        pagination.setTotal(page.getTotal());
        // 请假申请表持久化对象 转 请假申请表查询响应对象
        return leaveApplyPOAssembler.toQueryResponse(page.getRecords());
    }

    @Override
    public LeaveApply queryById(Long leaveApplyId) {
        // 根据主键查询请假申请表
        LeaveApplyPO leaveApplyPO = super.getById(leaveApplyId);
        // 请假申请表持久化对象 转 请假申请表响应对象
        return leaveApplyPOAssembler.toEntity(leaveApplyPO);
    }

    @Override
    public void create(LeaveApply leaveApply) {
        // 请假申请表实体转请假申请表持久化对象
        LeaveApplyPO leaveApplyPO = leaveApplyPOAssembler.assembler(leaveApply);

        // 创建请假申请表
        super.save(leaveApplyPO);
    }

    @Override
    public void deleteById(Long leaveApplyId) {
        // 物理删除请假申请表
        super.removeById(leaveApplyId);
    }

    @Override
    public void updateById(LeaveApply leaveApply) {
        // 请假申请表实体转请假申请表持久化对象
        LeaveApplyPO leaveApplyPO = leaveApplyPOAssembler.assembler(leaveApply);
        // 更新请假申请表
        super.updateById(leaveApplyPO);
    }
}
